from django.db.models import Q
from django.shortcuts import render
from rest_framework.generics import ListCreateAPIView
from rest_framework.views import APIView,Response
from rest_framework import request,status
from myapp.models import *
from myapp.views import MyPagination
from utils.login_util import operation_log
from .serializer import *



# 角色管理
class RoleAPIView(ListCreateAPIView):
    queryset = RoleModel.objects.filter(logic_del = 0).all()
    serializer_class = RoleSer
    pagination_class = MyPagination

    def post(self,request):
        name = request.data.get('name')
        comment = request.data.get('comment')
        jurisdiction = request.data.get('jurisdiction')

        s = jurisdiction[1:-1]
        s1 = s.split(',')
        if not all([name,comment]):
            return Response({'code':status.HTTP_400_BAD_REQUEST,'msg':'数据不完整'})

        role = RoleModel.objects.filter(name=name)
        if role:
            operation_log(request, '添加了一个角色')
            return Response({'code':status.HTTP_400_BAD_REQUEST,'msg':'角色已存在'})

        RoleModel.objects.create(name=name,comment=comment)
        for i in s1:
            role = RoleModel.objects.get(name=name)
            RoleMenuModel.objects.create(role_id=role.id,menu_id=i)

        return Response({'code':status.HTTP_200_OK,'msg':'添加成功'})

# 获取单个角色
class RoleAPI(APIView):
    def get(self,request,pk):
        role = RoleModel.objects.get(id=pk)
        if role.logic_del ==1:
            return Response({'code':status.HTTP_204_NO_CONTENT,'msg':'没有该角色'})
        role_ser = RoleSer(role).data
        return Response({"code": status.HTTP_200_OK, 'msg': '获取成功', 'data': role_ser})

    def delete(self,request,pk):
        role = RoleModel.objects.filter(Q(id=pk)&Q(logic_del=0)).update(logic_del = 1)
        if role:
            operation_log(request, '删除了一个角色')
            return Response({'code':status.HTTP_200_OK,'msg':'删除成功'})
        return Response({"code":status.HTTP_204_NO_CONTENT,'msg':'没有该角色'})

    def put(self,request,pk):
        name = request.data.get('name')
        comment = request.data.get('comment')

        if not all([name,comment]):
            return Response({'code':status.HTTP_400_BAD_REQUEST,'msg':'数据不完整'})

        role = RoleModel.objects.filter(Q(id=pk)&Q(logic_del=0))
        if not role:
            return Response({'code':status.HTTP_400_BAD_REQUEST,'msg':'角色不存在'})

        RoleModel.objects.filter(id=pk).update(name=name,comment=comment)
        operation_log(request, '修改了一个角色信息')
        return Response({'code':status.HTTP_200_OK,'msg':'修改成功'})

# 角色的模糊查询
class RoleVague(APIView):
    def post(self, request):
        name = request.data.get('name')

        role = RoleModel.objects.filter(name__contains=name).all()
        role_ser = RoleSer(role, many=True).data
        return Response({'code': status.HTTP_200_OK, 'msg': '获取成功', 'data': role_ser})

# 职级管理
class RankAPIView(APIView):
    queryset = RankModel.objects.filter(logic_del = 0).all()
    serializer_class = RankSer
    pagination_class = MyPagination
    def post(self,request):
        r_name = request.data.get('r_name')
        r_comment = request.data.get('r_comment')

        if not all([r_name,r_comment]):
            return Response({'code':status.HTTP_400_BAD_REQUEST,'msg':'数据不完整'})

        rank = RankModel.objects.filter(r_name=r_name)
        if rank:
            return Response({'code': status.HTTP_400_BAD_REQUEST, 'msg': '职级已存在'})

        RankModel.objects.create(r_name=r_name, r_comment=r_comment)
        operation_log(request, '添加了一个职级')
        return Response({'code': status.HTTP_200_OK, 'msg': '添加成功'})

# 单个职级
class RankAPI(APIView):
    def get(self, request, pk):
        rank = RankModel.objects.get(id=pk)
        if rank.logic_del == 1:
            return Response({'code': status.HTTP_204_NO_CONTENT, 'msg': '没有该职级'})
        role_ser = RankSer(rank).data
        return Response({"code": status.HTTP_200_OK, 'msg': '获取成功', 'data': role_ser})

    def delete(self, request, pk):
        rank = RankModel.objects.filter(Q(id=pk) & Q(logic_del=0)).update(logic_del=1)
        if rank:
            operation_log(request, '删除了一个职级')
            return Response({'code': status.HTTP_200_OK, 'msg': '删除成功'})
        return Response({"code": status.HTTP_204_NO_CONTENT, 'msg': '没有该用户'})

    def put(self, request, pk):
        r_name = request.data.get('r_name')
        r_comment = request.data.get('r_comment')

        if not all([r_name, r_comment]):
            return Response({'code': status.HTTP_400_BAD_REQUEST, 'msg': '数据不完整'})

        rank = RankModel.objects.filter(Q(id=pk) & Q(logic_del=0))
        if not rank:
            return Response({'code': status.HTTP_400_BAD_REQUEST, 'msg': '角色不存在'})

        RankModel.objects.filter(id=pk).update(r_name=r_name, r_comment=r_comment)
        operation_log(request, '修改了一个职级')
        return Response({'code': status.HTTP_200_OK, 'msg': '修改成功'})

# 职级的模糊查询
class RankVague(APIView):
    def post(self, request):
        r_name = request.data.get('r_name')

        rank = RankModel.objects.filter(r_name__contains=r_name).all()
        rank_ser = RankSer(rank, many=True).data
        return Response({'code': status.HTTP_200_OK, 'msg': '获取成功', 'data': rank_ser})

# 管理部门
class DepartmentAPIView(APIView):
    def get(self, request):
        department = DepartmentModel.objects.filter(logic_del=0).all()
        department_ser = DepartmentSer(department, many=True).data
        return Response({"code": status.HTTP_200_OK, 'msg': '获取成功', 'data': department_ser})

    def post(self, request):
        d_name = request.data.get('d_name')
        d_comment = request.data.get('d_comment')
        assoc = request.data.get('assoc')
        if not all([d_name, d_comment]):
            return Response({'code': status.HTTP_400_BAD_REQUEST, 'msg': '数据不完整'})

        department = DepartmentModel.objects.filter(d_name=d_name)
        if department:
            return Response({'code': status.HTTP_400_BAD_REQUEST, 'msg': '部门已存在'})
        if assoc:
            DepartmentModel.objects.create(d_name=d_name, d_comment=d_comment,assoc_id=assoc)
            operation_log(request, '添加了一个子部门')
            return Response({'code': status.HTTP_200_OK, 'msg': '添加成功'})
        DepartmentModel.objects.create(d_name=d_name, d_comment=d_comment)
        operation_log(request, '添加了一个父部门')
        return Response({'code': status.HTTP_200_OK, 'msg': '添加成功'})

# 单个部门
class DepartmentAPI(APIView):
    def get(self, request, pk):
        department = DepartmentModel.objects.get(id=pk)
        if department.logic_del == 1:
            return Response({'code': status.HTTP_204_NO_CONTENT, 'msg': '没有该部门'})
        department_ser = DepartmentSer(department).data
        return Response({"code": status.HTTP_200_OK, 'msg': '获取成功', 'data': department_ser})

    def delete(self, request, pk):
        department = DepartmentModel.objects.filter(Q(id=pk) & Q(logic_del=0)).update(logic_del=1)
        if department:
            operation_log(request, '删除了一个部门')
            return Response({'code': status.HTTP_200_OK, 'msg': '删除成功'})
        return Response({"code": status.HTTP_204_NO_CONTENT, 'msg': '没有该部门'})

    def put(self, request, pk):
        d_name = request.data.get('d_name')
        d_comment = request.data.get('d_comment')
        assoc = request.data.get('assoc')

        if not all([d_name, d_comment]):
            return Response({'code': status.HTTP_400_BAD_REQUEST, 'msg': '数据不完整'})

        department = DepartmentModel.objects.filter(Q(id=pk) & Q(logic_del=0))
        if not department:
            return Response({'code': status.HTTP_400_BAD_REQUEST, 'msg': '部门不存在'})
        if assoc:
            DepartmentModel.objects.filter(id=pk).update(d_name=d_name, d_comment=d_comment,assoc_id=assoc)
            operation_log(request, '修改了一个部门')
            return Response({'code': status.HTTP_200_OK, 'msg': '修改成功'})
        DepartmentModel.objects.filter(id=pk).update(d_name=d_name, d_comment=d_comment)
        return Response({'code': status.HTTP_200_OK, 'msg': '修改成功'})

# 部门的模糊查询
class DepartmentVague(APIView):
    def post(self, request):
        d_name = request.data.get('d_name')

        department = DepartmentModel.objects.filter(d_name__contains=d_name).all()
        department_ser = DepartmentSer(department, many=True).data
        return Response({'code': status.HTTP_200_OK, 'msg': '获取成功', 'data': department_ser})

# 岗位管理
class JobsAPIView(ListCreateAPIView):
    queryset = JobsModel.objects.filter(logic_del=0).all()
    serializer_class = JobsSer
    pagination_class = MyPagination

    def post(self, request):
        j_name = request.data.get('j_name')
        j_comment = request.data.get('j_comment')
        assoc = request.data.get('assoc')
        if not all([j_name, j_comment]):
            return Response({'code': status.HTTP_400_BAD_REQUEST, 'msg': '数据不完整'})

        jobs = JobsModel.objects.filter(j_name=j_name)
        if jobs:
            return Response({'code': status.HTTP_400_BAD_REQUEST, 'msg': '岗位已存在'})
        if assoc:
            JobsModel.objects.create(j_name=j_name, j_comment=j_comment, assoc_id=assoc)
            operation_log(request, '添加了一个子岗位')
            return Response({'code': status.HTTP_200_OK, 'msg': '添加成功'})
        JobsModel.objects.create(j_name=j_name, j_comment=j_comment)
        operation_log(request, '添加了一个父岗位')
        return Response({'code': status.HTTP_200_OK, 'msg': '添加成功'})

# 单个岗位
class JobsAPI(APIView):
    def get(self, request, pk):
        jobs = JobsModel.objects.get(id=pk)
        if jobs.logic_del == 1:
            return Response({'code': status.HTTP_204_NO_CONTENT, 'msg': '没有该岗位'})
        jobs_ser = JobsSer(jobs).data
        return Response({"code": status.HTTP_200_OK, 'msg': '获取成功', 'data': jobs_ser})

    def delete(self, request, pk):
        jobs = JobsModel.objects.filter(Q(id=pk) & Q(logic_del=0)).update(logic_del=1)
        if jobs:
            operation_log(request, '删除了一个岗位')
            return Response({'code': status.HTTP_200_OK, 'msg': '删除成功'})
        return Response({"code": status.HTTP_204_NO_CONTENT, 'msg': '没有该岗位'})

    def put(self, request, pk):
        j_name = request.data.get('j_name')
        j_comment = request.data.get('j_comment')
        assoc = request.data.get('assoc')

        if not all([j_name, j_comment]):
            return Response({'code': status.HTTP_400_BAD_REQUEST, 'msg': '数据不完整'})

        jobs = JobsModel.objects.filter(Q(id=pk) & Q(logic_del=0))
        if not jobs:
            return Response({'code': status.HTTP_400_BAD_REQUEST, 'msg': '岗位不存在'})
        if assoc:
            JobsModel.objects.filter(id=pk).update(j_name=j_name, j_comment=j_comment,assoc_id=assoc)
            operation_log(request, '修改了一个子岗位')
            return Response({'code': status.HTTP_200_OK, 'msg': '修改成功'})
        JobsModel.objects.filter(id=pk).update(j_name=j_name, j_comment=j_comment)
        operation_log(request, '修改了一个父岗位')
        return Response({'code': status.HTTP_200_OK, 'msg': '修改成功'})

# 岗位的模糊查询
class JobsVague(APIView):
    def post(self, request):
        j_name = request.data.get('j_name')

        jobs = JobsModel.objects.filter(j_name__contains=j_name).all()
        jobs_ser = JobsSer(jobs, many=True).data
        return Response({'code': status.HTTP_200_OK, 'msg': '获取成功', 'data': jobs_ser})
